using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMud.MudLib.Common.Environment.Persistance
{
    public interface IAreaLoadResults<TAreaInstance> : IAreaLoadResults where TAreaInstance : IArea, new()
    {
        new TAreaInstance LoadedArea { get;}
    }
    public class AreaLoadResults<TAreaInstance> : IAreaLoadResults<TAreaInstance> where TAreaInstance : IArea, new()
    {
        protected TAreaInstance         _LoadedArea;
        protected List<IAreaLoadMessage>  _AreaLoadMessages;

        protected AreaLoadResults()
        {
            _AreaLoadMessages = new List<IAreaLoadMessage>();
        }
        public AreaLoadResults(TAreaInstance loadedArea, params IAreaLoadMessage[] areaLoadMessages)
        {
            _LoadedArea = loadedArea;
            _AreaLoadMessages = new List<IAreaLoadMessage>(areaLoadMessages);
        }

        #region IAreaLoadResults<TAreaInstance> Members

        public TAreaInstance LoadedArea
        {
            get { return _LoadedArea; }
        }
        TAreaInstance IAreaLoadResults<TAreaInstance>.LoadedArea
        {
            get { return _LoadedArea; }
        }

        #endregion

        #region IAreaLoadResults Members

        public IAreaLoadMessage[] Messages
        {
            get { return _AreaLoadMessages.ToArray(); }
        }

        IArea IAreaLoadResults.LoadedArea
        {
            get
            {
                return (IArea)_LoadedArea;
            }
        }

        #endregion
    }

    public interface IAreaLoadResults
    {
        IAreaLoadMessage[]  Messages        { get;}
        IArea               LoadedArea      { get;}
    }
    public interface IAreaLoadMessage
    {
        string FileName { get;}
        int Row { get;}
        int Column { get;}
        string Message { get;}
        bool IsCritical { get;}
    }
    public class AreaLoadMessage : IAreaLoadMessage
    {
        private System.Exception                        _InnerException;
        private System.CodeDom.Compiler.CompilerError   _InnerCompilerError;

        private string _FileName;
        private int _Row;
        private int _Column;
        private string _Message;
        private bool _IsCritical;

        private AreaLoadMessage()
        {
        }

        public AreaLoadMessage(string message, string fileName, int row, int column, bool isCritical)
        {
            _Message = message;
            _FileName = fileName;
            _Row = row;
            _Column = column;
            _IsCritical = isCritical;
        }

        #region IAreaLoadMessage Members

        public string FileName
        {
            get { return _FileName; }
        }

        public int Row
        {
            get { return _Row; }
        }

        public int Column
        {
            get { return _Column; }
        }

        public string Message
        {
            get { return _Message; }
        }

        public bool IsCritical
        {
            get { return _IsCritical; }
        }

        #endregion

        public static IAreaLoadMessage FromException(System.Exception exception)
        {
            AreaLoadMessage alm = new AreaLoadMessage(exception.Message, exception.Source, -1, -1, true);
            alm._InnerException = exception;
            return alm;
        }

        public static IAreaLoadMessage FromCompilerError(System.CodeDom.Compiler.CompilerError compilerError)
        {
            AreaLoadMessage alm = new AreaLoadMessage(compilerError.ErrorText, compilerError.FileName, compilerError.Line, compilerError.Column, !compilerError.IsWarning);
            alm._InnerCompilerError = compilerError;
            return alm;
        }
    }
}
